package com.stox.client.ui.javafx.chartingy.model.chart;

import com.stox.client.ui.javafx.chartingy.model.axis.DateAxis;
import com.stox.client.ui.javafx.chartingy.model.plot.Plot;
import com.stox.client.ui.javafx.chartingy.model.plot.PricePlot;
import com.stox.client.ui.javafx.chartingy.view.ChartingView;
import com.stox.client.ui.javafx.chartingy.widget.BarInfoPanel;
import com.stox.client.ui.javafx.widget.RangeSlider;
import com.stox.shared.model.Bar;
import com.stox.shared.model.Scrip;

public class PrimaryChart extends Chart<Scrip, Bar> {

	private final DateAxis dateAxis = new DateAxis(this);
	private final BarInfoPanel barInfoPanel = new BarInfoPanel(this);

	public PrimaryChart(final ChartingView chartingView) {
		super(chartingView);
		setBottom(dateAxis);
		getGlassPane().getChildren().add(barInfoPanel);
	}

	@Override
	public void updateAxisBounds() {
		final Plot<Scrip, Bar> primaryPlot = getPrimaryPlot();
		final RangeSlider rangeSlider = getChartingView().getRangeSlider();
		if (null != primaryPlot) {
			final int size = primaryPlot.getData().size();
			final int lowerBoundIndex = (int) ((1 - rangeSlider.getLowValue()) * (size - 1));
			final int upperBoundIndex = (int) ((1 - rangeSlider.getHighValue()) * (size - 1));
			dateAxis.updateBoundIndices(upperBoundIndex, lowerBoundIndex);
			super.updateAxisBounds();
		}
	}

	public DateAxis getDateAxis() {
		return dateAxis;
	}

	@Override
	public PricePlot getPrimaryPlot() {
		return (PricePlot) super.getPrimaryPlot();
	}

	public BarInfoPanel getBarInfoPanel() {
		return barInfoPanel;
	}

}
